package com.stoneframe.controller.manageSystem;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stoneframe.controller.base.BaseGenericController;
import com.stoneframe.entity.other.TreeEntity;
import com.stoneframe.entity.other.TreeGridEntity;
import com.stoneframe.service.BaseService;
import com.stoneframe.service.manageSystem.SysDataItemService;
import com.stoneframe.util.baseApp.AppHelper;
import com.stoneframe.util.baseApp.PageData;
import com.stoneframe.util.baseJson.JsonHelper;
import com.stoneframe.util.baseJson.TreeJsonHelper;

@Controller
@RequestMapping(value="sysDataItem")
public class SysDataItemController extends BaseGenericController {
	
	@Resource(name="sysDataItemService")
	private SysDataItemService sysDataItemService;

	@Override
	public BaseService getService() {
		// TODO Auto-generated method stub
		return sysDataItemService;
	}
	
	/**
	 * 树JONS
	 */
	@ResponseBody
	@RequestMapping(value="treeJson",produces= "text/plain;charset=UTF-8")
	public Object treeJson(){
		logBefore(logger, className+"treeJson(树)");
		
		PageData pd=this.getPageData();
		List<PageData> list=null;
		List<TreeEntity> treeList = new ArrayList();
		try{
			list=sysDataItemService.listAll(pd);
			for(PageData sysModulePageData:list){
				TreeEntity tree=new TreeEntity();
				Boolean hasChildren =false;
				for(PageData subModulePageData:list){
					if(subModulePageData.getString("parentId").equals(sysModulePageData.getString("itemId"))){
						hasChildren=true;
						break;
					}
				}
                tree.setId(sysModulePageData.getString("itemId"));
                tree.setText(sysModulePageData.getString("itemName"));
                tree.setValue(sysModulePageData.getString("itemId"));
                tree.setIsexpand(true);
                tree.setComplete(true);
                tree.setHasChildren(hasChildren);
                tree.setParentId(sysModulePageData.getString("parentId"));
                tree.setAttribute("isTree");
                tree.setAttributeValue(sysModulePageData.get("isTree").toString());
				
				treeList.add(tree);
			}
			
		}catch(Exception e){
			logger.error(e.toString(),e);
			return AppHelper.errorObject(e.getMessage());
		}

		logAfter(logger);
		return AppHelper.returnObject(pd,TreeJsonHelper.treeToJson(treeList, "0"));
	}
	
	/**
	 * 树GridJson
	 */
	@ResponseBody
	@RequestMapping(value="treeGridJson",produces= "text/plain;charset=UTF-8")
	public Object treeGridJson(){
		logBefore(logger, className+"treeGridJson");
		
		PageData pd=this.getPageData();
		List<PageData> list=null;
		List<TreeGridEntity> treeGridList=new ArrayList();
		try{
			list=sysDataItemService.listAll(pd);
			for(PageData item : list){
				TreeGridEntity treeGrid = new TreeGridEntity();
                Boolean hasChildren =false;
                for(PageData subItem:list){
                	if(item.getString("itemId").equals(subItem.getString("parentId"))){
                		hasChildren=true;
                		break;
                	}
                }
                treeGrid.setId(item.getString("itemId"));
                treeGrid.setHasChildren(hasChildren);
                treeGrid.setParentId(item.getString("parentId"));
                treeGrid.setExpanded(true);
                treeGrid.setEntityJson(JsonHelper.toJsonStr(item));
                treeGridList.add(treeGrid);
			}
			
			logAfter(logger);
			return AppHelper.returnObject(pd, TreeJsonHelper.treeGridToJson(treeGridList));
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			return AppHelper.errorObject(e.getMessage());
		}
	}

}
